package com.i_art_engineer.meigong.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.adapter.PhrasesListAdapter;
import com.i_art_engineer.meigong.base.FullScreenActivity;
import com.i_art_engineer.meigong.widget.AlertDialog;
import com.i_art_engineer.meigong.widget.InputDialog;

public class CantActivity extends FullScreenActivity {

    private ListView cantlist;
    private ImageView iv_back;
    private TextView tv_add;

    private ArrayList<String> data = new ArrayList<>();

    PhrasesListAdapter mAdapter;

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ImageView iv_edit = (ImageView) view.findViewById(R.id.iv_phrase_edit);
            final int loc = position;
            iv_edit.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    new InputDialog(CantActivity.this).builder().setMsg(data.get(loc)).setPositiveButton(
                            getString(R.string.btn_ok),
                            new InputDialog.OnInputClickListener() {
                                @Override
                                public void onClick(String v) {
                                    data.set(loc, v);
                                    mAdapter.notifyDataSetChanged();

                                }
                            }).setNegativeButton("取消", null).show();
                }
            });
            ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_phrase_delete);
            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog(CantActivity.this).builder().setTitle(getString(R.string.dlg_title_hint)).setMsg
                            ("删除后无法恢复，\n是否确认删除这条常用语？").setPositiveButton(getString(R.string.btn_ok),
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    data.remove(loc);
                                    mAdapter.notifyDataSetChanged();
                                }
                            }).setNegativeButton(getString(R.string.btn_cancel), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();
                }
            });

        }
    };

    public void showTextInputDialog() {
        String input = "";

        //显示水印提示信息
        new InputDialog(CantActivity.this).builder().setMsg(input).setPositiveButton(
                getString(R.string.btn_ok),
                new InputDialog.OnInputClickListener() {
                    @Override
                    public void onClick(String v) {
                        data.add(v);
                        mAdapter.notifyDataSetChanged();

                    }
                }).setNegativeButton("取消", null).show();
    }

    View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_back:
                    finish();
                    break;
                case R.id.newPhrase:
                    showTextInputDialog();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cant);

        String[] cants = getSharedPreference("cants");

        for (int i = 0; i < cants.length; i++) {
            data.add(cants[i]);
        }

        cantlist = (ListView) findViewById(R.id.list_cantlist);

        mAdapter = new PhrasesListAdapter(this);
        mAdapter.setData(data);
        cantlist.setAdapter(mAdapter);
        cantlist.setOnItemClickListener(itemClickListener);

        iv_back = (ImageView) findViewById(R.id.btn_back);
        tv_add = (TextView) findViewById(R.id.newPhrase);
        iv_back.setOnClickListener(listener);
        tv_add.setOnClickListener(listener);
    }

    public String[] getSharedPreference(String key) {
        String regularEx = "爩";
        String[] str = null;

        SharedPreferences sp = getSharedPreferences("profiles", Activity.MODE_PRIVATE);
        String values = sp.getString(key, "");
        str = values.split(regularEx);

        return str;
    }

    public void setSharedPreference(String key, String[] values) {
        String regularEx = "爩";
        String str = "";
        SharedPreferences sp = getSharedPreferences("profiles", Activity.MODE_PRIVATE);
        if (values != null && values.length > 0) {
            for (String value : values) {
                str += value;
                str += regularEx;
            }
            sp.edit().putString(key, str).commit();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        String[] cants = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {
            cants[i] = data.get(i);
        }
        setSharedPreference("cants", cants);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String[] cants = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {
            cants[i] = data.get(i);
        }
        setSharedPreference("cants", cants);
//        outState.putString("cants", "cants");
    }

}
